package com.das.mechanic_base.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.login.X3AreaAdapter;
import com.das.mechanic_base.bean.login.AreaBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class X3AreaDialog {

    private View view;
    private X3MaxHeightRecyclerView rlv_area;
    private ProgressBar pb_bar;
    private X3AreaAdapter areaAdapter;
    private Dialog dialog;

    private Context mContext;

    public void showOtherDialog(Context context) {
        //1、使用Dialog、设置style
        this.mContext = context;
        dialog = new Dialog(context, R.style.dialog);
        //2、设置布局
        view = View.inflate(context, R.layout.x3_area_item, null);
        dialog.setContentView(view);

        pb_bar = view.findViewById(R.id.pb_bar);
        rlv_area = view.findViewById(R.id.rlv_area);
        rlv_area.setLayoutManager(new LinearLayoutManager(context));
        areaAdapter = new X3AreaAdapter(context);
        rlv_area.setAdapter(areaAdapter);
        areaAdapter.setiOnClickArea((area, imgUrl,localUrl,localH5Url) -> {
            // 选择区号
            if (dialog != null) {

                dialog.dismiss();
            }
            if (onClickSelectArea != null) {
                onClickSelectArea.iOnSelectArea(area,imgUrl,localUrl,localH5Url);
            }
        });
        dialog.setOnDismissListener(dialog -> {
            MobclickAgent.onPageEnd("区号列表弹窗");
        });

        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);

        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = X3ScreenUtils.getScreenWidth(mContext);
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);

    }

    IOnClickSelectArea onClickSelectArea;

    public void setOnClickSelectArea(IOnClickSelectArea onClickSelectArea) {
        this.onClickSelectArea = onClickSelectArea;
    }

    public interface IOnClickSelectArea{
        void iOnSelectArea(String area,String imgUrl,String localUrl,String localH5Url);
    }

    public void showPrograssBar() {
        if (pb_bar != null) {
            pb_bar.setVisibility(View.VISIBLE);
        }
    }

    public void show() {
        if (dialog != null) {
            dialog.show();
            MobclickAgent.onPageStart("区号列表弹窗");
        }
    }

    /**
     * 暂时区号
     * @param mList
     * @param select
     */
    public void changeArea(List<AreaBean> mList, String select) {
        pb_bar.setVisibility(View.GONE);
        if (!X3StringUtils.isListEmpty(mList) && areaAdapter != null) {
            areaAdapter.changeData(mList,select);
        }
    }

}
